
import { _decorator, Component, Game, instantiate, Node, size } from 'cc';
import { AppLauncher } from './AppLauncher';
import { BattleSceneCtrl,BattleReport,BattleRoundReport,AttackReport,AttackTarget, BattleLineupData } from './BattleSceneCtrl';
import { ByteBuffer } from './ByteBuffer';
import {CharactorData} from './CharactorCtrl'
const { ccclass, property } = _decorator;

@ccclass('GameCtrl')
export class GameCtrl extends Component {
    
    static Instance : GameCtrl = null;

    //opcode func
    opcodeCBFunc : any = null;

    protected onLoad(): void {
        if(!GameCtrl.Instance)
        {
            GameCtrl.Instance = this;
        }
    }

    start () {
        this.RegisterOpcodeCBFunc();
    }

    RegisterOpcodeCBFunc(){

        this.opcodeCBFunc = {
            1 : this.BattleStage,
            2 : this.SetBattleLineup
        }

    }

    GetCBFuncByOpcode(opcode : number){
        return this.opcodeCBFunc[opcode]
    }

    SetBattleLineup(buffer : any){

        let battleScene = instantiate(AppLauncher.Instance.UIBattleScene);
        let battleLineup : BattleLineupData = {
            blueLineup : [],
            redLineup : []
        }

        let ReadCharactorData = (lineup : CharactorData[],buffer : any)=>{
            let blue = buffer.readUint8();
            {
                let size = buffer.readUint32();
                for(let i=0;i<size;++i)
                {
                    let charactorData : CharactorData= {
                        uid : 0,
                        heroId : 0,
                        curHp : 0,
                        maxHp : 0,
                        speed : 0,
                        curAtk : 0,
                        anger : 0,
                        def : 0,
                        avoid : 0,
                        critical : 0,
                        qualtity : 0,
                        vGift : [],
                        vSkill : [],
                        atkSkill : 0,
                        combat : 0
                    };
    
                    charactorData.uid = buffer.readUint32();
                    charactorData.heroId = buffer.readUint32();
                    charactorData.curHp = buffer.readUint32();
                    charactorData.maxHp = buffer.readUint32();
                    charactorData.speed = buffer.readUint32();
                    charactorData.curAtk = buffer.readUint32();
                    charactorData.anger = buffer.readUint32();
                    charactorData.def = buffer.readUint32();
                    charactorData.avoid = buffer.readUint32();
                    charactorData.critical = buffer.readUint32();
                    charactorData.qualtity = buffer.readUint8();
                    console.log(" curHp: ",charactorData.curHp," maxHp: ",charactorData.curHp)

                    let giftSize = buffer.readUint32();
                    for(let i=0;i<giftSize;++i)
                    {
                        let gift = buffer.readUint32();
                        charactorData.vGift.push(gift);
                    }
                    let skillSize = buffer.readUint32();
                    for(let i=0;i<skillSize;++i)
                    {
                        let skill = buffer.readUint32();
                        charactorData.vSkill.push(skill);
                    }
                    charactorData.atkSkill = buffer.readUint32();
                    charactorData.combat = buffer.readUint32();
    
                    lineup.push(charactorData)
                }
            }
        };

        ReadCharactorData(battleLineup.blueLineup,buffer);
        ReadCharactorData(battleLineup.redLineup,buffer);

        battleScene.getComponent("BattleSceneCtrl").SetBattleLineup(battleLineup);
        //console.log(battleScene.name)
        AppLauncher.Instance.Canvas.addChild(battleScene);
    }

    BattleStage(buffer : any){
        // let UIBattleField = instantiate(AppLauncher.Instance.UIBattleField);
        // AppLauncher.Instance.Canvas.addChild(UIBattleField)

        //let battleScene = instantiate(AppLauncher.Instance.UIBattleScene);
        let battleScene = AppLauncher.Instance.Canvas.getChildByName("BattleSceneCtrl");

        let report : BattleReport = {
            isRedWin : false,
            roundCount : 0,
            vReport : []
        };

        //解析数据，填充战报
        report.isRedWin = buffer.readUint8();
        report.roundCount = buffer.readUint32();

        var size = buffer.readUint32();
        for(let i=0;i<size;++i)
        {
            var size2 = buffer.readUint32();
            let roundReport : BattleRoundReport = { 
                atkReport : []
            }
            for(let j=0;j<size2;++j)
            {
                let atkReport : AttackReport = {
                    atkId : 0,
                    vTarget : [],
                    atkSkillId : 0
                }
                atkReport.atkId = buffer.readUint32();
                var size3 = buffer.readUint32();
                for(let k=0;k<size3;++k)
                {
                    let atkTarget : AttackTarget = {
                        targetId : 0,
                        effectId : 0,
                        value : 0,
                        fieldType : 0,
                        flag : 0,
                        round : 0,
                    }
                    atkTarget.targetId = buffer.readUint32();
                    atkTarget.effectId = buffer.readUint32();
                    atkTarget.value = buffer.readInt32();
                    atkTarget.fieldType = buffer.readUint32();
                    atkTarget.flag = buffer.readUint8();
                    atkTarget.round = buffer.readUint8();

                    atkReport.vTarget.push(atkTarget);
                }
                atkReport.atkSkillId = buffer.readUint32();

                roundReport.atkReport.push(atkReport);
            }
            report.vReport.push(roundReport);
        }

        let battle = battleScene.getComponent("BattleSceneCtrl");

        battle.InitBattleReport(report);

        battle.StartBattle();

        //AppLauncher.Instance.Canvas.addChild(battleScene);
    }

}